#include "volume_utils.h"

	void load_mhd_header(const std::string& fn, pde::Size& size, pde::Spacing& spacing, pde::Origin& origin, std::string& extra_info)
	{
		// Read file content
		std::ifstream inputStream;
		inputStream.open(fn.c_str(), std::ios::in);
		if (inputStream.fail())
		{
			fprintf(stderr, "open file failed: %s.\n", fn.c_str());
			return;
		}

		char* buf = new char[8001];
		inputStream.read(buf, 8000);
		unsigned long fileSize = (unsigned long)inputStream.gcount();
		buf[fileSize] = 0;
		std::string header(buf);
		header.resize(fileSize);
		delete[] buf;
		inputStream.close();

		// Check content
		std::string value;
		std::stringstream ss;
		//Matrix3x3 orientation;
		//double* orien_ptr = glm::value_ptr(orientation);
		bool big_endian = false;



		value = pde::GetTagValue(header, "Offset");
		if (value != "") {
			ss.clear();
			ss << value;
			for (int i = 0; i < 3; ++i) {
				ss >> origin[i];
			}
		}

		value = pde::GetTagValue(header, "ElementSpacing");
		if (value != "") {
			ss.clear();
			ss << value;
			for (int i = 0; i < 3; ++i) {
				ss >> spacing[i];
			}
		}

		value = pde::GetTagValue(header, "DimSize");
		if (value != "") {
			ss.clear();
			ss << value;
			for (int i = 0; i < 3; ++i) {
				ss >> size[i];
			}
		}

		value = pde::GetTagValue(header, "BinaryDataByteOrderMSB");
		if (value != "") {
			if (value == "True") {
				big_endian = true;
			}
		}

		extra_info = pde::GetTagValue(header, "ExtraInfo");

	}
